-- noraml
-- created by panyl
-- 地牢格子：通用建筑格子

-- 随机一下数组/两两交换
local function randomTable(arr)
    for oriIndex, value in pairs(arr) do
        local toIndex = math.random(#arr);
        local temp = arr[toIndex];
        arr[toIndex] = arr[oriIndex];
        arr[oriIndex] = temp;
    end
    return arr;
end

return {
    autoFight = function(grid)
        local excludeType = {GRID_TYPE_ARENA_FLAG, GRID_TYPE_ARENA_BOARD,
        GRID_TYPE_MONSTER_BUILDING, GRID_TYPE_PICTURE};
        if table.indexOf(excludeType, grid.type) ~= -1 then
            -- 不需要操作类型
            return false;
        end

        local dungeonId = DungeonM.getDungeonId();
        local layer = DungeonM.currentLayer();
        if DungeonBossM.isBossLayer(dungeonId, layer)
            and not DungeonM.isAllMonsterDead() then
            -- boss层要等boss被击杀
            return false;
        end

        local pos = grid:getPos();
        if grid.type == GRID_TYPE_COFFIN then
            -- 圣棺
            local bonusList = grid.bonus;
            local index = math.random(#bonusList);
            return DungeonActionM.go("take_coffin_bonus", pos, index);
        elseif grid.type == GRID_TYPE_EAST_SHRINE then
            -- 东方的神龛
            local items = {7255, 8156, 8155, 8106, 7216, 7055, 7254, 8065};
            items = randomTable(items);
            for _, classId in pairs(items) do
                if DungeonActionM.go("east_shrine", pos, classId) then
                    return true;
                end
            end
        elseif grid.type == GRID_TYPE_MACHINE_ALTAR then
            -- 机械祭坛
            local items = {5249, 5250, 7006, 7021, 7051, 7068, 7077, 7119,
                7208, 7212, 7213, 7214, 7219, 7316, 7317, 7318, 7352, 8060, 8061, 8064, 8136};
            items = randomTable(items);
            for _, classId in pairs(items) do
                if DungeonActionM.go("machine_altar", pos, classId) then
                    return true;
                end
            end
        elseif grid.type == GRID_TYPE_OLD_STRONGBOX then
            -- 古老的保险箱
            return DungeonActionM.go("old_strongbox_bonus", pos);
        elseif grid.type == GRID_TYPE_CAVE_HIDE_ITEM then
            -- 洞窟隐藏物件
            return DungeonActionM.go("do_building_bonus", pos);
        else
            -- 通用
            if DungeonInstanceM.isInInstance() then
                -- 如果在副本中，走副本奖励流程
                return DungeonActionM.go("take_instance_bonus", pos);
            else
                return DungeonActionM.go("do_building_bonus", pos);
            end
        end

        return false;
    end,
};
